package com.li.controller;

import com.li.pojo.ReturnMessage;
import com.li.util.MyTimeUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
//        @RequestMapping("/upload")
//        public String upload(MultipartFile file, HttpServletRequest req) {
//            String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
//            String format = MyTimeUtil.getCurrentTime();
//            File folder = new File(realPath + format);
//            String filePath="";
//            if (!folder.isDirectory()) {
//                folder.mkdirs();
//                String oldName = file.getOriginalFilename();
//                String newName = UUID.randomUUID() +
//                        oldName.substring(oldName.lastIndexOf("."), oldName.length());
//                try {
//                    file.transferTo(new File(folder, newName));
//                    filePath = req.getScheme() + "://" + req.getServerName() + ":" +
//                            req.getServerPort() + "/uploadFile/" + format + newName;
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return "上传失败! ";
//                }
//            }
//            return filePath;
//        }
@PostMapping("/upload")
public @ResponseBody ReturnMessage upload(@RequestParam MultipartFile file, HttpServletRequest request){
    ReturnMessage returnMessage= new ReturnMessage();
    if(!file.isEmpty()){
        String uploadPath = "/tmp/javaUpload";
        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        String OriginalFilename = file.getOriginalFilename();//获取原文件名
        String suffixName = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));//获取文件后缀名
        //重新随机生成名字
        String filename = UUID.randomUUID().toString() +suffixName;
        File localFile = new File(uploadPath+"/"+filename);
        try {
            file.transferTo(localFile); //把上传的文件保存至本地
            /**
             * 这里应该把filename保存到数据库,供前端访问时使用
             */
            returnMessage.setMsg("上传成功！");
            returnMessage.setData("/images/"+filename);//上传成功，返回保存的文件地址
            return returnMessage;
        }catch (IOException e){
            e.printStackTrace();
            returnMessage.setState("400");
            returnMessage.setMsg("上传失败,请重试");
            System.out.println("上传失败");
            return returnMessage;
        }
    }else{
        System.out.println("文件为空");
        returnMessage.setState("400");
        returnMessage.setMsg("文件为空");
        return returnMessage;
    }
}
//    @PostMapping(value = "/upload")
//    public String upload(HttpServletRequest req,  MultipartFile file) {
//
//        try {
//            String fileName=req.getServletContext().getRealPath("uploaded")+File.separator+file.getOriginalFilename(); //multipartFile.getOriginalFilename()可以获得文件的原始名称。req.getServletContext().getRealPath("uploaded")可以获得src/main/webapp/uploaded所在的目录,这个目录是前端获取静态资源时的默认目录
//            File file1 = new File(fileName);
//            file1.getParentFile().mkdirs();
//            if(file == null) {
//                System.out.println("file is null");
//            }else {
//                file.transferTo(file1);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            return "上传失败," + e.getMessage();
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "上传失败," + e.getMessage();
//        }
//        return "success";
//    }






}


